package com.example.peachgrove.controller.admin.user;

import com.example.peachgrove.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    JWTUtils jwtUtils;

    @Value("${Login.username}")
    private String username;

    @Value("${Login.password}")
    private String password;

    @PostMapping("/login")
    @ResponseBody
    private HashMap login(HttpServletRequest request, HttpServletResponse response){
        HashMap res = new HashMap();

        if (username==request.getParameter("username")&&password ==request.getParameter("password")){
            String token =jwtUtils.getToken(request.getParameter("username"),request.getParameter("password"));
            response.setHeader("token",token);
            res.put("code","200");
            res.put("msg","登陆成功");
            res.put("token",token);
        }else {
            res.put("code","501");
            res.put("msg","登陆失败");
            res.put("token","");
        }
        return res;
    }
}
